{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "03685769-a7db-48eb-b590-e83465b85f1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4797b85-9bef-46f4-9cf9-5c28f21e7e26",
   "metadata": {},
   "source": [
    "二进制读写文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "98be7422-99a6-465d-8d1b-a0a3f505ca18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.67684263, 0.47107625, 0.11237606, 0.13236877],\n",
       "       [0.9436458 , 0.22466772, 0.40388788, 0.25395407],\n",
       "       [0.46517705, 0.44018968, 0.95501286, 0.34033983]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.random((3,4))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ca04cd89-7f03-4ed1-b692-a8c67b4864c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save(\"./temp1.npy\",a) # 扩展名是npy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a4fd72e7-f46b-43d7-b5e8-547ff03c5975",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.random.random((2,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "64124f17-c8c3-488a-8f34-6410a18050d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savez(\"./temp2.npz\",a,b) # 扩展名是npz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6851ebac-076f-42fa-95d4-eff73ae489de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.67684263, 0.47107625, 0.11237606, 0.13236877],\n",
       "       [0.9436458 , 0.22466772, 0.40388788, 0.25395407],\n",
       "       [0.46517705, 0.44018968, 0.95501286, 0.34033983]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load(\"./temp1.npy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cdf1efad-f391-4c6f-ba08-d92f6d238d17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<numpy.lib.npyio.NpzFile at 0x1f8e0b22c10>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load(\"./temp2.npz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5a0d0c03-eba9-4bfc-9882-8c5021aab885",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = np.load(\"./temp2.npz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c8772d58-d628-410e-a7dd-2c1f2610c6a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['arr_0', 'arr_1']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.files "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e983b83b-540c-4c7a-96ac-cdb83629d8e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.67684263, 0.47107625, 0.11237606, 0.13236877],\n",
       "       [0.9436458 , 0.22466772, 0.40388788, 0.25395407],\n",
       "       [0.46517705, 0.44018968, 0.95501286, 0.34033983]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['arr_0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6580a19a-9555-4ea5-8f3a-69351832cef1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.47010323, 0.16737871],\n",
       "       [0.81059983, 0.44615159]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['arr_1']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc04ac85-787b-4496-a529-4faa1386ea9b",
   "metadata": {},
   "source": [
    "文本读取文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "38422370-bfb1-4538-9e28-97c1a7010a1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('./temp1.txt',a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "77ea7392-a137-4915-8740-0d2c59fcbc43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.67684263, 0.47107625, 0.11237606, 0.13236877],\n",
       "       [0.9436458 , 0.22466772, 0.40388788, 0.25395407],\n",
       "       [0.46517705, 0.44018968, 0.95501286, 0.34033983]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('./temp1.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c8ed5313-3054-4d06-8a48-13161ae2b6b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.67684263, 0.47107625, 0.11237606, 0.13236877],\n",
       "       [0.9436458 , 0.22466772, 0.40388788, 0.25395407],\n",
       "       [0.46517705, 0.44018968, 0.95501286, 0.34033983]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.genfromtxt('./temp1.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c2a97853-e01c-4047-b47f-8ca5b6516017",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('./temp1.csv',a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4d195189-2637-4ad2-afaf-b3b2b3863c2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.67684263, 0.47107625, 0.11237606, 0.13236877],\n",
       "       [0.9436458 , 0.22466772, 0.40388788, 0.25395407],\n",
       "       [0.46517705, 0.44018968, 0.95501286, 0.34033983]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('./temp1.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc45951c-e665-4768-9ca1-8f100fea7d29",
   "metadata": {},
   "source": [
    "统计分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cf4998b6-3f4f-442d-bac3-40758116e315",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 6, 7, 4, 3, 7, 3, 2, 9, 3])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(1,10,size=10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "73167e08-10ae-4635-a7ef-8689996dba47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 3, 3, 4, 6, 7, 7, 9])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sort()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "14034728-a933-4d67-bc0e-ba1363ddd6b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 4],\n",
       "       [8, 6, 6],\n",
       "       [3, 2, 8]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(1,10,size=(3,3))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "cc212fb1-18dd-4735-bb35-e12557a78eb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 4],\n",
       "       [6, 6, 8],\n",
       "       [2, 3, 8]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sort(axis=1)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "02bfe9f6-ae8d-43a6-83c3-b38d4e5d0c9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 4],\n",
       "       [3, 4, 8],\n",
       "       [6, 6, 8]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sort(axis=0)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8dc01a61-ccdd-4d33-8152-35c46ed6a9ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = np.array([2,4,5,9,1,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "355f8992-75b9-46de-9552-8d43f2a93749",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 4, 0, 1, 2, 3], dtype=int64)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.argsort() # 返回值为重新排列后的小标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4797a9fe-043f-4ffa-90e4-6a3eaa13f094",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([9,7,1,3,5])\n",
    "b = np.array([20,50,60,30,10])\n",
    "c = np.array([300,200,800,100,900])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "cc007030-0246-4b4f-ac78-b47de31bae36",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = np.lexsort((a,b,c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "35cb6ab0-80df-4a4d-9d9e-164b0c701f1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(3, 30, 100), (7, 50, 200), (9, 20, 300), (1, 60, 800), (5, 10, 900)]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(a[d],b[d],c[d]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7395056d-c42e-4beb-b5e1-3afabf2089ac",
   "metadata": {},
   "source": [
    "重复与去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "ffe1aa02-cc8f-4bc7-a217-8a8b32d0f779",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 2, 1, 5, 2, 9, 3, 1, 4, 4])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(1,10,size=10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "5d0b6cb4-430e-4b4e-be27-a42a32d1e205",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 9])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(a) # 去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "0ba20e0b-4630-433b-9ce2-6e9d7dad7c63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 3, 2, 2, 1, 1, 5, 5, 2, 2, 9, 9, 3, 3, 1, 1, 4, 4, 4, 4])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(a,2) # 数据重复(按照元素)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "44393270-1977-4f75-868e-cc5d7ec021b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 2, 1, 5, 2, 9, 3, 1, 4, 4, 3, 2, 1, 5, 2, 9, 3, 1, 4, 4])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tile(a,2) # 数据重复(按照数组)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "c8de23a1-8c4c-4f30-97f6-29dc91801bdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 4, 7],\n",
       "       [7, 9, 3],\n",
       "       [1, 7, 9]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(1,10,size=(3,3))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6e998ef7-a243-4012-8f1e-c89b0029b1e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 4, 7],\n",
       "       [9, 4, 7],\n",
       "       [7, 9, 3],\n",
       "       [7, 9, 3],\n",
       "       [1, 7, 9],\n",
       "       [1, 7, 9]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.repeat(2,axis=0) # 按照横轴重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "8a1acec2-7178-4099-a9b0-0fe6b851e254",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 4, 4, 7, 7],\n",
       "       [7, 7, 9, 9, 3, 3],\n",
       "       [1, 1, 7, 7, 9, 9]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.repeat(2,axis=1) # 按照纵轴进行重复"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6773ef78-eb7a-49df-b668-ac551deae9e2",
   "metadata": {},
   "source": [
    "统计函数\n",
    "* 常用统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "840825c1-5e17-4ed8-a9dd-fdb2b3c683ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(20).reshape(4,5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "fed86b35-23d5-423d-aeb4-65cb3d4171ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "190"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(a) # 求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "66af9e37-0308-4bf1-8e56-507dac0381d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.5"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(a) # 均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "ff629673-7fac-4716-bed6-c5eb747c6a4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(a) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04c5e553-b2a6-43e9-a494-d0eb8f9cec20",
   "metadata": {},
   "source": [
    "* cumsum&cumprod函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "d91f50dc-6f69-4d54-99ef-8fed10b94ae5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(2,10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "67d62176-0888-4223-87c1-81a5500b9017",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  5,  9, 14, 20, 27, 35, 44])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cumsum(a) # 计算所有元素的累计和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "52102d8f-58fb-4748-97e4-ecfd977d28e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([     2,      6,     24,    120,    720,   5040,  40320, 362880])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cumprod(a)# 计算所有元素的累计积"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
